import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.*;

public class RecordsForm extends Form implements CommandListener 
{

    private static final Command CMD_NEW = new Command("New", Command.ITEM, 1);
    private static final Command CMD_DELETE = new Command("Delete", Command.ITEM, 1);

    private Table table;
    public Display display;
    public List recordsList;
	private RecordStore rs;
	public int records = 0;

    public RecordsForm(Table table, Display display) 
	{
		super("Select the record");

        this.table = table;
        this.display = display;

		// Open record store
		try
		{
			rs = RecordStore.openRecordStore("SpreadSheet",true);	
					
			records = rs.getNumRecords();
			if(records > 0)
			{
				String[] values = new String[records];
				for(int k=1; k <= records; k++)
				{
					byte buffer[] = rs.getRecord(k);
					String recordData = new String(buffer);
					values[k-1] = recordData;
				}

				recordsList = new List("Records", Choice.IMPLICIT, values, null);
				recordsList.addCommand(CMD_NEW);
				recordsList.addCommand(CMD_DELETE);
				recordsList.setCommandListener(this);
			}

				rs.closeRecordStore();
		}
		catch(RecordStoreNotOpenException exc)
		{
		}
		catch(InvalidRecordIDException exc)
		{
		}	
		catch(RecordStoreException exc)
		{
		}
		catch(NullPointerException exc)
		{
		}
    }

    public void commandAction(Command c, Displayable d) 
    {
		if(c == CMD_NEW)
		{
            display.setCurrentItem(table);
		}
		else if(c == CMD_DELETE)
		{
			int index = ((List)d).getSelectedIndex();
			String selectedOption = ((List)d).getString(index);
			((List)d).delete(index);
			try
			{
				RecordStore.deleteRecordStore(selectedOption);
				rs = RecordStore.openRecordStore("SpreadSheet",true);	

				String[] values = new String[1];
				byte buffer[];
				records = rs.getNumRecords();
				if(records > 0)
				{
					values = new String[records];
					for(int k=1; k <= records; k++)
					{
						if(k-1 != index)
						{
							buffer = rs.getRecord(k);
							String recordData = new String(buffer);
							values[k-1] = recordData;
						}
					}
				}

				rs.closeRecordStore();
				RecordStore.deleteRecordStore("SpreadSheet");
				rs = RecordStore.openRecordStore("SpreadSheet",true);	
				for(int i=0;i<values.length ;i++)
				{
					if(values[i] != null)
					{
						String currentData = values[i];
						buffer = currentData.getBytes();
						rs.addRecord(buffer,0,buffer.length);	
					}
				}
				rs.closeRecordStore();
			}
			catch(RecordStoreNotOpenException exc)
			{
			}
			catch(InvalidRecordIDException exc)
			{
			}	
			catch(RecordStoreException exc)
			{
			}
			catch(NullPointerException exc)
			{
			}
		}
		else if(d.equals(recordsList))
		{
			int index = ((List)d).getSelectedIndex();
			String selectedOption = ((List)d).getString(index);
			table.openRecordStore(selectedOption);
            display.setCurrentItem(table);		
		}
	}
}
